package org.example.juniortest;

import java.io.*;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

public class FileTool {
    String fileName="Student.txt";

    public static File createFile(String fileName, Student student) throws IOException {
        File file = new File(fileName);
        if (!file.exists()) {
            file.createNewFile();
        }
        return file;
    }

    public static void writeFile(String file, String data) throws IOException {
        FileWriter fileWriter=new FileWriter(file);
        fileWriter.write(data);
        fileWriter.close();
    }

    public static List<Student> readFile(String file) throws IOException {
        FileReader fileReader=new FileReader(file);
        List<Student> list =new ArrayList<>();
        BufferedReader bufferedReader=new BufferedReader(fileReader);
        String str=null;
        while ((str=bufferedReader.readLine())!=null){
            Student student=new Student();
            String[] array=str.split(",");
            student.setName(array[0]);
            student.setAge(Integer.parseInt(array[1]));
            student.setHeight(new BigDecimal(array[2]));
            student.setWeight(new BigDecimal(array[3]));
            student.setClassroom(array[4]);
            list.add(student);
        }
        return list;
    }
}
